function toDetails(path,name,id,parm=''){

	hui.loading();
	//api.closeFrame()

	//id = api.pageParam.id;
	//alert(id+"="+parm)
	api.openWin({
		name: name,
		url: path+name+"_win.html",
		pageParam:{
			id:id,
			parm:parm,
		}
	})
	hui.closeLoading();
}

function closefrm(){
	api.closeFrame()
}

function closewin(){
	api.closeWin()
}

//患者导航
function setfooter(tier){
	
	var html = '<a href="javascript:void(0);" onclick="home('+tier+')" id="nav0">';
	html += '<div class="hui-footer-icons iconfont iconshouye"></div>';
	html += '<div class="hui-footer-text">首页</div>';
	html += '</a>';
	html += '<a href="javascript:void(0);" onclick="doctor('+tier+')" id="nav1">';
	html += '<div class="hui-footer-icons iconfont iconyisheng1"></div>';
	html += '<div class="hui-footer-text">医生</div>';
	html += '</a>';
	html += '<div style="width:20%; text-align:center;" onclick="up('+tier+')" class="hui-fl" id="nav4">';
	html += '<div class="hui-footer-icons iconfont iconyiyuan" style="color:#4FB9FF;height:45px;line-height:45px;;font-size:35px;"></div>';
	html += '</div>';
	html += '<a href="javascript:void(0);" onclick="danger('+tier+')" id="nav2">';
	html += '<div class="hui-footer-icons iconfont iconyujing"></div>';
	html += '<div class="hui-footer-text">预警</div>';
	html += '</a>';
	html += '<a href="javascript:void(0);" onclick="my('+tier+')" id="nav3">';
	html += '<div class="hui-footer-icons iconfont iconwode1"></div>';
	html += '<div class="hui-footer-text">我的</div>';
	html += '</a>';

	hui("#hui-footer").html(html);
}


//设置颜色
function navxh(cur) {
	for (var i = 0; i < 5; i++) {
		if (i == cur) {
			hui("#nav"+i).addClass("hui-footer-active");
		} else {
			hui("#nav"+i).removeClass("hui-footer-active");
		}
	}
}

function home(tier){

	hui.loading();
	//api.closeFrame()	
	id = api.pageParam.id;
	//if (tier==0) {
	//	toDetails('../patient/home/','home',0);
	//} else {
	toDetails('../home/','home',0);
	//}
	
	navxh(0)
	hui.closeLoading();
}


function danger(tier){
	hui.loading();
	//if (tier==0) {
	//	toDetails('../patient/up/','up_record_danger',0);
	//} else {
		toDetails('../up/','up_record_danger',0);
	//}
	navxh(2);
	hui.closeLoading();
}

function up(tier){
	hui.loading();	
	//if (tier==0) {
	//	toDetails('../patient/up/','up',0);
	//} else {
		toDetails('../up/','up',0);
	//}
	navxh(4);
	hui.closeLoading();
}

function doctor(tier){
	hui.loading();
	//if (tier==0) {
	//	toDetails('../patient/doctor/','list',0);
	//} else {
		toDetails('../doctor/','list',0);
	//}	
	
	navxh(1);
	hui.closeLoading();
}

function my(tier){
	hui.loading();
	//if (tier==0) {
	//	toDetails('','my',0);
	//} else {
		toDetails('../my/','my',0);
	//}
	navxh(3);
	hui.closeLoading();
}

//医生端导航
function setfooter2(tier){
	
	var html = '<a href="javascript:void(0);" onclick="home2('+tier+')" id="nav0">';
	html += '<div class="hui-footer-icons iconfont iconshouye"></div>';
	html += '<div class="hui-footer-text">首页</div>';
	html += '</a>';
	html += '<a href="javascript:void(0);" onclick="doctor2('+tier+')" id="nav1">';
	html += '<div class="hui-footer-icons iconfont iconyisheng1"></div>';
	html += '<div class="hui-footer-text">患者</div>';
	html += '</a>';
	html += '<div style="width:20%; text-align:center;" onclick="up2('+tier+')" class="hui-fl" id="nav4">';
	html += '<div class="hui-footer-icons iconfont iconyiyuan" style="color:#4FB9FF;height:45px;line-height:45px;font-size:35px;"></div>';
	html += '</div>';
	html += '<a href="javascript:void(0);" onclick="danger2('+tier+')" id="nav2">';
	html += '<div class="hui-footer-icons iconfont iconyujing"></div>';
	html += '<div class="hui-footer-text">预警</div>';
	html += '</a>';
	html += '<a href="javascript:void(0);" onclick="my2('+tier+')" id="nav3">';
	html += '<div class="hui-footer-icons iconfont iconwode1"></div>';
	html += '<div class="hui-footer-text">我的</div>';
	html += '</a>';

	hui("#hui-footer").html(html);
}

function home2(tier){

	hui.loading();
	//api.closeFrame()	
	id = api.pageParam.id;
	
	//if (tier==0) {
	//	toDetails('../doctor/home/','home',0);
	//} else {
		toDetails('../home/','home',0);
	//}
	navxh(0)
	hui.closeLoading();
}


function danger2(tier){
	hui.loading();
	//if (tier==0) {
	//	toDetails('../doctor/up/','up_record_danger',0);
	//}else{
		toDetails('../up/','up_record',0);
	//}
	navxh(2);
	hui.closeLoading();
}

function up2(tier){
	hui.loading();
	//if (tier==0) {
	///	toDetails('../doctor/up/','up',0);
	//}else{
		toDetails('../up/','up',0);
	//}
	navxh(4);
	hui.closeLoading();
}

function doctor2(tier){
	hui.loading();
	//if (tier==0) {
	//	toDetails('../doctor/doctor/','list',0);
	//}else{
		toDetails('../doctor/','list',0);
	//}
	navxh(1);
	hui.closeLoading();
}

function my2(tier){
	hui.loading();
	//if (tier==0) {
	//	toDetails('','my',0);
	//}else{
		toDetails('../my/','my',0);
	//}
	navxh(3);
	hui.closeLoading();
}

function showupdata(){

	var mnPopups = api.require('MNPopups');
	mnPopups.open({
		rect: {
			w: 100,
			h: 290
		},
		position: {
			x: api.winWidth - 25,
			y: 50
		},
		styles: {
			mask: 'rgba(0,0,0,0.2)',
			bg: '#666',
			cell: {
				bg: {
					normal: '',
					highlight: ''
				},
				h: 45,
				title: {
					marginL: 45,
					color: '#fff',
					size: 12,
				},
				icon: {
					marginL: 10,
					w: 25,
					h: 25,
					corner: 2
				}
			},
			pointer: {
				size: 7,
				xPercent: 90,
				yPercent: 0,
				orientation: 'downward'
			}
		},
		datas: [{
			title: '上报记录',
			icon: 'fs://MNPopups/addFriends.png'
		}, {
			title: '我要取药',
			icon: 'fs://MNPopups/scan.png'
		}, {
			title: '取药记录',
			icon: 'fs://MNPopups/scan.png'
		}, {
			title: '预警信息',
			icon: 'fs://MNPopups/send.png'
		}, {
			title: '上报信息',
			icon: 'fs://MNPopups/send.png'
		}, {
			title: '慢性肺阻',
			icon: 'fs://MNPopups/send.png'
		}],
		animation: true
	}, function(ret) {
		if (ret) {
			if(ret.eventType=='click'){
				if(ret.index==0){
					//alert("你点击了第一个按钮")
					toDetails('./','up_record',0,'')
				}else if(ret.index==1){
					toDetails('./','up_drug',0,'')
				}else if(ret.index==2){
					toDetails('./','up_drug_record',0,'')

				}else if(ret.index==3){
					toDetails('./','up_record_danger',0,'')

				}else if(ret.index==4){
					toDetails('./','up',0,'')

				}else if(ret.index==4){
					toDetails('./','up',0,'')

				}
			}

		}
	});
}


function showupdatadoc(){

	var mnPopups = api.require('MNPopups');
	mnPopups.open({
		rect: {
			w: 100,
			h: 200
		},
		position: {
			x: api.winWidth - 25,
			y: 50
		},
		styles: {
			mask: 'rgba(0,0,0,0.2)',
			bg: '#666',
			cell: {
				bg: {
					normal: '',
					highlight: ''
				},
				h: 45,
				title: {
					marginL: 45,
					color: '#fff',
					size: 12,
				},
				icon: {
					marginL: 10,
					w: 25,
					h: 25,
					corner: 2
				}
			},
			pointer: {
				size: 7,
				xPercent: 90,
				yPercent: 0,
				orientation: 'downward'
			}
		},
		datas: [{
			title: '上报数据',
			icon: 'fs://MNPopups/send.png'
		},{
			title: '上报记录',
			icon: 'fs://MNPopups/addFriends.png'
		}, {
			title: '患者取药',
			icon: 'fs://MNPopups/scan.png'
		}, {
			title: '取药记录',
			icon: 'fs://MNPopups/scan.png'
		}],
		animation: true
	}, function(ret) {
		if (ret) {
			if(ret.eventType=='click'){
				if(ret.index==0){
					//alert("你点击了第一个按钮")
					toDetails('./','up',0,'')					
					
				}else if(ret.index==1){
					toDetails('./','up_record',0,'')

				}else if(ret.index==2){
					toDetails('./','up_drug',0,'')

				}else if(ret.index==3){
					toDetails('./','up_drug_record',0,'')

				}
			}

		}
	});
}

hui("#hui-back").click(function(){
	api.closeWin()
	//history.back(-1)
});

function reload(){
	window.location.reload(); 
}

function saoma(){

	var FNScanner = api.require('FNScanner');
	FNScanner.open({
		autorotation: true
	}, function(ret, err) {
		if (ret) {
			if(ret.openWin=='success'){

				api.openFrame({
					name: 'reg',
					url: '../../../reg.html',
					rect: {
						x: 0,
						y: 0,
					},
					pageParam: {
						docid: ret.content
					},
				});

			}
		} else {
			alert(JSON.stringify(err));
		}
	});
}

//首页扫码注册
function saoma2(){
	//api.closeFrame()
	var FNScanner = api.require('FNScanner');
	FNScanner.open({
		autorotation: true
	}, function(ret, err) {
		if (ret) {
			if(ret.eventType=='success'){
				
				api.execScript({
					frameName: 'reg',
					script: 'setval("'+ret.content+'");'
				});
				
			}
			
		} else {
			alert(JSON.stringify(err));
		}
	});
}






















//判断网络
function worknet(){
	var connectionType = api.connectionType;

	if(connectionType=='wifi'){
		
	}else if(connectionType=='4G'){
		$api.toast('温馨提示','当前是4G网络，需要消耗流量，网速快。',2000);
		//api.toast({msg:'数据出错……', location: 'middle'});//JSON.stringify(err)
	}else if(connectionType=='3G'){
		$api.toast('温馨提示','当前3G网络，需要消耗流量，网速一般。',2000);
	}else if(connectionType=='2G'){
		$api.toast('温馨提示','当前是2G网络，网速过慢，网速慢。',2000);
	}else{
		$api.toast('温馨提示','没有网络，请查看网络连接。',2000);
	}
}


//用户登录
function getuserinfo(){
	var username = document.getElementById('username').value;
	var password = document.getElementById('password').value;
	api.ajax({
		url : $api.getStorage('weburl')+"index.php?c=app&a=getuserinfo&password="+password+"&username="+username,
		method : 'get',
	}, function(ret, err) {
		if (ret) {
			$api.setStorage('username',ret.username);
			$api.setStorage('nickname',ret.nickname);
			$api.setStorage('userid',ret.id);
			$api.setStorage('email',ret.email);
			$api.setStorage('avatar',ret.avatar);
			$api.setStorage('groupid',ret.groupid);
			$api.setStorage('modelid',ret.modelid);
			$api.setStorage('credits',ret.credits);
			$api.setStorage('regdate',ret.regdate);
			$api.setStorage('regip',ret.regip);
			$api.setStorage('lastloginip',ret.lastloginip);
			$api.setStorage('lastlogintime',ret.lastlogintime);
			$api.setStorage('loginip',ret.loginip);
			$api.setStorage('logintime',ret.logintime);
			document.getElementById("userlogin").style.display = "none";
			document.getElementById("sendcomment").style.display = "block";
		} else {
			alert('用户名密码不正确，登录失败。');  //JSON.stringify(err)
		}
	});
}


function islogin(tag){

	//判断页面需不需要登录
	if(tag==0){
		return false;
	}
	//判断是否已经登录
	if($api.getStorage('userid')=='' || $api.getStorage('userid')==null || typeof($api.getStorage('userid'))=='undefined'){
		if(tag==2){

			api.openFrame({
				name: 'index',
				url : './index.html',
				bounces: false,
				rect: { // 推荐使用Margin布局，用于适配屏幕的动态变化
					marginTop: 0, // main页面距离win顶部的高度
					marginBottom: 0, // main页面距离win底部的高度
					w: 'auto' // main页面的宽度 自适应屏幕宽度
				}
			});

		}else{

			api.openFrame({
				name: 'index',
				url : '../../../index.html',
				bounces: false,
				rect: { // 推荐使用Margin布局，用于适配屏幕的动态变化
					marginTop: 0, // main页面距离win顶部的高度
					marginBottom: 0, // main页面距离win底部的高度
					w: 'auto' // main页面的宽度 自适应屏幕宽度
				}
			});

		}
		
	}

}

function loginout(){

	$api.setStorage('userid','')
	$api.setStorage('docid','')
	$api.setStorage('hospid','')
	$api.setStorage('flag',0)
	alert('退出成功')
	api.openWin({
		name: 'index',
		url: "../../../index.html",
		pageParam:{

		}
	})

}




function post(url,param={},debug=0){

	if (url=='') {
		api.toast({ msg:'地址参数不正确',location:'top' })
		return false;
	}

	api.ajax({
        url  : $api.getStorage('geturl')+url,
        method: 'post',
        data: {
            values: param
        }
    },function(res, err){
        if (res) {	
            if (res.status==1) {
				if(debug==1){
					console.log(JSON.stringify(res.data));
					alert(JSON.stringify(res.data));
				}
				$api.setStorage('data',res.data);
            }else{
                api.toast({ msg:res.tip,location:'top' })
            }
        } else {
            alert( JSON.stringify( err ) );
        }
    });

}

function returnpost(json){
	return json;
}




//以下是自定义函数
function jsDateDiff(publishTime,type){  

    var d_minutes,d_hours,d_days;       
    var timeNow = parseInt(new Date().getTime()/1000);       
    var d;       
    d = timeNow - publishTime;       
    d_days = parseInt(d/86400);       
    d_hours = parseInt(d/3600);       
    d_minutes = parseInt(d/60);       
    if(d_days>0 && d_days<4){       
        return d_days+"天前";       
    }else if(d_days<=0 && d_hours>0){       
        return d_hours+"小时前";       
    }else if(d_hours<=0 && d_minutes>0){       
        return d_minutes+"分钟前";       
    }else{       
        var s = new Date(publishTime*1000);       
        // s.getFullYear()+"年";
		var month = s.getMonth()+1;
		if (month<10) {
			month = "0"+month
		}
		var day = s.getDate();
		if (day<10) {
			day = "0"+day
		}
		var hour = s.getHours();
		if (hour<10) {
			hour = "0"+hour
		}
		var minutes = s.getMinutes();
		if (minutes<10) {
			minutes = "0"+minutes
		}

		if(type==1){
			return s.getFullYear()+"-"+(month)+"-"+day+" "+hour+":"+minutes;
		}else{
			return s.getFullYear()+"-"+(month)+"-"+day;
		}
               
    }       
}
		
		
function getLocalTime(nS) { 
	return new Date(parseInt(nS) * 1000).toLocaleString().substr(0,17)
}


function formatSeconds(value) { 
	var theTime = parseInt(value);// 秒 
	var theTime1 = 0;// 分 
	var theTime2 = 0;// 小时 
	
	if(theTime > 60) { 
		theTime1 = parseInt(theTime/60); 
		theTime = parseInt(theTime%60); 
		// alert(theTime1+"-"+theTime); 
		if(theTime1 > 60) { 
			theTime2 = parseInt(theTime1/60); 
			theTime1 = parseInt(theTime1%60); 
		} 
	}
	
	var result = ""+PrefixInteger(parseInt(theTime),2); 
	if(theTime1 > 0) { 
		result = ""+PrefixInteger(parseInt(theTime1),2)+":"+result; 
	}else{
		result = "00:"+result; 
	}
	if(theTime2 > 0) { 
		result = ""+parseInt(theTime2)+":"+result; 
	}else{
		result = "0:"+result;
	}
	if(result==0){
		result = "0:00:00";
	}
	return result; 
}

//num传入的数字，n需要的字符长度
function PrefixInteger(num, n) {
	return (Array(n).join(0) + num).slice(-n);
}